﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using MyBlog.DataAccess;
using MyBlog.Model;
using MyBlog.Common;
using AutoMapper;
using MyBlog.BusinessInterface;
using MyBlog.DataEntity;
using MyBlog.DataAccessInterface;
namespace MyBlog.Business
{
    public class BusinessPicComment : IBusinessPicComment
    {
        IDALPicComment _dalComment;
        IUnitOfWork _unitWork;
        public BusinessPicComment(IDALPicComment dalComment, IUnitOfWork unitWork)
        {
            _dalComment = dalComment;
            _unitWork = unitWork;
        }
        public void Add(PicCommentModel model)
        {
            var dataEntity = Mapper.Map<Tbl_PicComment>(model);
            _dalComment.Add(dataEntity);
            model.ID = dataEntity.ID;
            _unitWork.Commit();
        }

        public void Update(PicCommentModel model)
        {
            var dataEntity = Mapper.Map<Tbl_PicComment>(model);
            _dalComment.Update(dataEntity);
            _unitWork.Commit();
        }
        public void AddRange(IEnumerable<PicCommentModel> models)
        {
            var dataEntitys = Mapper.Map<List<Tbl_PicComment>>(models);
            _dalComment.AddRange(dataEntitys);
            _unitWork.Commit();
        }

        public void DeleteRange(IEnumerable<PicCommentModel> models)
        {
            var dataEntitys = Mapper.Map<List<Tbl_PicComment>>(models);
            _dalComment.DeleteRange(dataEntitys);
            _unitWork.Commit();
        }

        public List<PicCommentModel> GetAll(bool isTracking = true)
        {
            var entities = _dalComment.GetAll(isTracking);
            return Mapper.Map<List<PicCommentModel>>(entities);
        }

        public void Delete(params object[] primaryKey)
        {
            _dalComment.Delete(primaryKey);
            _unitWork.Commit();
        }

        public PicCommentModel Get(params object[] primaryKey)
        {
            var dataEntity = _dalComment.Get(primaryKey);
            return Mapper.Map<PicCommentModel>(dataEntity);
        }

        public void Delete(PicCommentModel model)
        {
            var where = LinqExpressionBuilder.True<Tbl_PicComment>();
            if (model.ID > 0)
            {
                where = where.And(x => x.ID == model.ID);
            }
            _dalComment.Delete(where);
            _unitWork.Commit();
        }

        public PicCommentModel Get(PicCommentModel model)
        {
            var where = LinqExpressionBuilder.True<Tbl_PicComment>();
            if (model.ID != 0)
            {
                where = where.And(x => x.ID == model.ID);
            }
            if (model.PicId != 0)
            {
                where = where.And(x => x.PicId == model.PicId);
            }
            var dataEntity = _dalComment.Get(where);
            return Mapper.Map<PicCommentModel>(dataEntity);
        }

        public List<PicCommentModel> GetAll(PicCommentModel model, bool isTracking = true)
        {
            var where = LinqExpressionBuilder.True<Tbl_PicComment>();
            if (model.PicId != 0)
            {
                where = where.And(x => x.PicId == model.PicId);
            }
            var entities = _dalComment.GetAll(where, isTracking);
            return Mapper.Map<List<PicCommentModel>>(entities);
        }


        public List<PicCommentModel> GetAll(PicCommentModel model, int page, int size, out int total, bool isTracking = true)
        {
            throw new NotImplementedException();
        }
    }
}
